#############################################################################
#  Copyright (C) 2007 Boscovich, Maximiliano - mboscovich@gmail.com          #
#                                                                            #
#  This program is free software; you can redistribute it and/or modify      #
#  it under the terms of the GNU General Public License as published by      #
#  the Free Software Foundation; either version 3 of the License, or         #
#  (at your option) any later version. See tht GPL_Licence.txt for details.  #
#                                                                            #
#  This program is distributed in the hope that it will be useful,           #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of            #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              #
#  GNU General Public License for more details.                              #
#                                                                            #
##############################################################################

import sys
sys.path.append("../")

from sqlobject import dberrors
from formencode import Invalid

from view.Architecture_view import *
from model.tables import *
import logModule

class Architecture_controller(Ui_Architecture_view):

    def __init__(self):
        self.widget = QtGui.QDialog()
        self.setupUi(self.widget)
        # Connections
        self.widget.connect(self.pushButtonOk, QtCore.SIGNAL("clicked()"), 
                            QtCore.SLOT('close()'))
        self.widget.connect(self.pushButtonAdd, QtCore.SIGNAL("clicked()"), 
                            self.addArchitecture)
        self.widget.connect(self.pushButtonDelete, QtCore.SIGNAL("clicked()"),
                            self.delArchitecture)
        self.load_list()
    
    # load all registers on listWidget
    def load_list(self):
        registers = Architecture.select()
        for register in registers:
            self.listWidgetArchitectures.addItem(register.Type)
    
    def addArchitecture(self):
        try:
            new_architecture = Architecture(
                                Type=str(self.lineEditAdd_Architecture.text())
                                )
            self.listWidgetArchitectures.addItem(new_architecture.Type) 
        except Invalid,e:
            logModule.log(e)
        except dberrors.DuplicateEntryError:
            logModule.log("La arquitectura "+
                        str(self.lineEditAdd_Architecture.text())+" ya existe"
                        )

    def delArchitecture(self):
        item_selected = (self.listWidgetArchitectures.currentItem())
        arch_selected = str(item_selected.text())
        arch_to_delete = Architecture.selectBy(Type=arch_selected)
        self.listWidgetArchitectures.takeItem(
                                self.listWidgetArchitectures.currentRow()
                                )
        arch_to_delete[0].destroySelf()
        
